var express = require('express');
var router = express.Router();
const { shangpinModel, shangpin2Model } = require('../model/model.js')
router.get('/shangpin', async (req, res) => {
  const obj = {}
  if (req.query.typetype) {
    obj.typetype = req.query.typetype
    if (obj.typetype == '全部') {
      delete obj.typetype
    }
  }
  const data = await shangpinModel.find(obj)
  if (req.query.paixu === '降序') {
    data.sort((a, b) => {
      return b.price - a.price
    })
  }
  if (req.query.paixu === '升序') {
    data.sort((a, b) => {
      return a.price - b.price
    })
  }

  console.log(data);
  res.send(data)
})
router.get('/list', async (req, res) => {
  let len = req.query.len
  data = await shangpin2Model.find().limit(len)
  let lens = (await shangpin2Model.find()).length
  if (lens > len) { res.send({ msg: "成功", data, hasMore: true, }) }
  else { res.send({ msg: "成功", data, hasMore: false }) }
});

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;
